#include "timeutil.h"

namespace UTIL
{

char* getDateStr(char *sdate, tm *ptm, const std::string &spliter)
{
    if (sdate == 0)
        sdate = utilTimeStr;
    const char *strSplit = spliter.c_str();
    assert(ptm);
    sprintf(sdate, "%d%s%02d%s%02d", ptm->tm_year + 1900, strSplit, ptm->tm_mon + 1, strSplit, ptm->tm_mday);
    return sdate;
}

char* getTimeStr(char *stime, tm *ptm, const std::string &spliter)
{
    if (stime == 0)
        stime = utilTimeStr;
    const char *strSplit = spliter.c_str();
    assert(ptm);
    sprintf(stime, "%02d%s%02d", ptm->tm_hour, strSplit, ptm->tm_min);
    return stime;
}

char* getDateTimeStr(char *sdatetime, tm *ptm, const std::string &spliter)
{
    if (sdatetime == 0)
        sdatetime = utilTimeStr;
    const char *strSplit = spliter.c_str();
    assert(ptm);
    sprintf(sdatetime, "%d%s%d%s%d %02d:%02d", ptm->tm_year + 1900, strSplit, ptm->tm_mon + 1, strSplit, ptm->tm_mday, ptm->tm_hour, ptm->tm_min);
    return sdatetime;
}

char* getDateStr(char *sdate, time_t time, const std::string &spliter)
{
    if (sdate == 0)
        sdate = utilTimeStr;
    tm *ptm = gmtime(&time);
    if (!ptm) {
        strcpy(sdate, "Error");
        return sdate;
    }
    return getDateStr(sdate, ptm, spliter);
}

char* getTimeStr(char *stime, time_t time, const std::string &spliter)
{
    if (stime == 0)
        stime = utilTimeStr;
    tm *ptm = gmtime(&time);
    if (!ptm) {
        strcpy(stime, "Error");
        return stime;
    }
    return getTimeStr(stime, ptm, spliter);
}

char* getDateTimeStr(char *sdatetime, time_t time, const std::string &spliter)
{
    if (sdatetime == 0)
        sdatetime = utilTimeStr;
    tm *ptm = gmtime(&time);
    if (!ptm) {
        strcpy(sdatetime, "Error");
        return sdatetime;
    }
    return getDateTimeStr(sdatetime, ptm, spliter);
}


char* getTimeSymbol(int time)
{
    switch (time) {
        case 1:
            strcpy(timeSymbol, "M1");
            break;
        case 5:
            strcpy(timeSymbol, "M5");
            break;
        case 15:
            strcpy(timeSymbol, "M15");
            break;
        case 30:
            strcpy(timeSymbol, "M30");
            break;
        case 60:
            strcpy(timeSymbol, "H1");
            break;
        case 240:
            strcpy(timeSymbol, "H4");
            break;
        case 1440:
            strcpy(timeSymbol, "D1");
            break;
        case 10080:
            strcpy(timeSymbol, "W1");
            break;
        case 40320:
            strcpy(timeSymbol, "WN");
            break;
        default:
            itoa(time, timeSymbol, 10);
    }
    return timeSymbol;
}
//
//time_t StrToDateTime(const char *sdate, const char *stime)
//{
//    if (strlen(sdate) == 0 || strlen(stime) == 0)
//        return 0;
//    int len = strlen(sdate) > strlen(stime) ? strlen(sdate) : strlen(stime);
//    char *tmpStr = new char[len];
//    tmpStr = removeNoNumber(str);
//    delete []tmpStr;
//}
//
//char* removeNoNumber(char *str)
//{
//    if (!str)
//        return 0;
//    int len = strlen(str), currPos = 0;
//    for (int i = 0 ; i < len; ++i) {
//        if (str[i] <= '9' && str[i] >= '0') {
//            str[currPos++] = str[i];
//        }
//    }
//    str[currPos] = '\0';
//}

}; // namespace